/**
 * Created by jocky on 2018/4/9.
 */
var orderObj = {
    /**
     * 持仓
     *
     * @param type
     * @param direction
     */
    position: function (type, direction) {
        //显示loading
        show_loading_table('order_position_lists_table', 10);

        var params = {};
        params.isRender = 2;

        var pageNo_dom = 'order_position_lists_pageNo';
        var pageTotal_dom = 'order_position_lists_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        var formObj = $('#order_positions_form');
        $.each(formObj.find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.getJSON('/order/position', params, function (json) {
            var t = json;
            $("#" + pageNo_dom).val(parseInt(t.pageNo));
            $("#" + pageTotal_dom).html(parseInt(t.pageTotal));

            var color = '';
            var sumProfit = parseFloat(t.sum.profit);
            if (sumProfit > 0) {
                color = 'red';
            } else {
                if (sumProfit < 0) {
                    color = 'green';
                }
            }
            var contracts = '<option value="-1">所有合约</option>';
            $.each(t.contracts, function (i, v) {
                var selected = v.instrumentID == t.contractID ? 'selected' : '';
                contracts += "<option value='" + v.instrumentID + "' " + selected + ">" + v.product + "[" + v.exchangeCode + "]</option>";
            });

            formObj.find('select[name=contract_id]').html(contracts);

            var tr_head = "<tr>" +
                "<td><i style='color: red;'>汇总</i></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td class='text-right'><i>" + parseInt(t.sum.volumes) + "</i></td>" +
                "<td class='text-right'><i>" + parseFloat(t.sum.deposit) + "</i></td>" +
                "<td></td>" +
                "</tr>";

            var tr = "";
            $.each(t.lists, function (i, v) {
                var color = '';
                var profit = parseFloat(v.profit);
                if (profit > 0) {
                    color = 'red';
                } else {
                    if (profit < 0) {
                        color = 'green';
                    }
                }

                var contract = t.contracts[v.contractID];
                var direction = v.direction == 1 ? "买" : "卖";
                tr += "<tr>";
                tr += "<td><a href='javascript:void(0)' title='查看 / 修改账户信息' onclick='traderObj.setForm(" + v.userID + ")'>" + v.clientSN + "</a></td>";
                tr += "<td>" + v.product + "[" + v.exchangeCode + "]</td>";
                tr += "<td>" + t.fixed_trade_unit + "</td>";
                tr += "<td class='text-center'><i>" + v.time + "</i></td>";
                tr += "<td class='text-center'>" + direction + "</td>";
                tr += "<td class='text-right'><i>" + parseInt(v.totalVolume) + "</i></td>";
                tr += "<td class='text-right'><i>" + parseFloat(v.deposit) + "</i></td>";
                tr += "<td class='text-right'><i>" + parseFloat(v.price) + "</i></td>";
                tr += "</tr>";
            });

            $("#order_position_lists_table").find('tbody').html(tr_head + tr);
        })
    },

    /**
     * 历史
     *
     * @param type
     * @param direction
     */
    history: function (type, direction) {
        //显示loading
        show_loading_table('order_history_lists_table', 11);

        var params = {};
        params.isRender = 2;
        var pageNo_dom = 'order_history_lists_pageNo';
        var pageTotal_dom = 'order_history_lists_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        var formObj = $('#order_history_form');
        $.each(formObj.find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.getJSON('/order/history', params, function (json) {
            var t = json;
            $("#" + pageNo_dom).val(parseInt(t.pageNo));
            $("#" + pageTotal_dom).html(parseInt(t.pageTotal));

            var colorSum = '';
            var sumProfit = parseFloat(t.sum.sumProfit);
            if (sumProfit > 0) {
                colorSum = 'red';
            } else {
                if (sumProfit < 0) {
                    colorSum = 'green';
                }
            }

            var tr_head = "<tr>" +
                "<td><i style='color: red;'>汇总</i></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td></td>" +
                "<td class='text-right'>" + t.sum.sumVolumes + "</td>" +
                "<td></td>" +
                "<td class='text-right'>" + t.sum.sumTradeCost + "</td>" +
                "<td class='text-right'><i style='color: " + colorSum + "'>" + sumProfit + "</i></td>";
            tr_head += "</tr>";

            var tr = "";
            $.each(t.lists, function (i, v) {
                var color = '';
                var profit = parseFloat(v.profit);
                if (profit > 0) {
                    color = 'red';
                } else {
                    if (profit < 0) {
                        color = 'green';
                    }
                }

                tr += "<tr>";
                tr += "<td><a href='javascript:void(0)' title='查看 / 修改账户信息' onclick='traderObj.setForm(" + v.uid + ")'>" + v.clientSN + "</a></td>";
                tr += "<td>" + v.product + "[" + v.exchangeCode + "]</td>";
                tr += "<td class='text-center'>" + (v.openTradeId != 0 ? '平' : '开') + "</td>";
                tr += "<td class='text-center'>" + v.time + "</td>";
                tr += "<td class='text-right'>" + parseFloat(v.volume) + "</td>";
                tr += "<td class='text-right'><i>" + parseFloat(v.price) + "</i></td>";
                tr += "<td class='text-right'><i>" + parseFloat(v.tradeCost) + "</i></td>";
                tr += "<td class='text-right'><i style='color: " + color + "'>" + (v.openTradeId != 0 ? profit : '--') + "</i></td>";
                tr += "</tr>";
            });

            $("#order_history_lists_table").find('tbody').html(tr_head + tr);
        })
    }
};